package com.tzstcl.sys.user.ctrl;

import com.github.pagehelper.PageInfo;
import com.tzstcl.base.ctrl.BaseCtrl;
import com.tzstcl.base.model.AjaxResult;
import com.tzstcl.framework.runner.DataDictInit;
import com.tzstcl.sys.user.model.SysDict;
import com.tzstcl.sys.user.service.impl.SysDictServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公司：天筑科技股份有限公司
 * 作者：haocj
 * 日期：2018年09月13日
 * 说明：字典管理Controller
 */
@Controller
@RequestMapping("/admin/sysDict")
public class SysDictCtrl extends BaseCtrl {

    @Autowired
    private SysDictServiceImpl sysDictServiceImpl;

    @Autowired
    private DataDictInit dataDictInit;

    /**
     * 新增 修改
     *
     * @param sysDict
     * @return
     */
    @RequestMapping("/save")
//    @RequiresPermissions(value={"sysDict:edite","sysDict:add"},logical=Logical.OR)
    @ResponseBody
    public AjaxResult save(@Valid SysDict sysDict) {
        return toAjax(sysDictServiceImpl.insertOrUpdate(sysDict));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sysDict:delete")
    @ResponseBody
    public AjaxResult delete(String ids) {
        return toAjax(sysDictServiceImpl.deleteBatchIds(ids));
    }

    /**
     * 获取单条信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/get")
//    @RequiresPermissions("sysDict:view")
    @ResponseBody
    public AjaxResult get(Long id) {
        return success("获取信息成功", sysDictServiceImpl.getOne(id));
    }

    /**
     * 获取查询的分页数据
     *
     * @param sysDict
     * @return
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sysDict:view")
    @ResponseBody
    public PageInfo<SysDict> list(SysDict sysDict) {
        return sysDictServiceImpl.selectPage(sysDict);
    }

    /**
     * list页面导航
     *
     * @return
     */
//    @RequiresPermissions("sysDict:view")
    @RequestMapping("/toList")
    public String toList() {
        return "admin/sys/sys_dict";
    }

    /**
     * form页面导航
     *
     * @return
     */
//    @RequiresPermissions(value={"sysDict:edite","sysDict:add"},logical=Logical.OR)
    @RequestMapping("/toForm")
    public String toForm() {
        return "sys/user/sysDictForm";
    }


    /**
     * 更新缓存中的字典数据
     *
     * @return
     */
    @RequestMapping("/updateDataDictCache")
    @ResponseBody
    public AjaxResult updateDataDictCache() {
        dataDictInit.initDataDict();
        return AjaxResult.success();
    }

    /**
     * 获取字典数据
     *
     * @param dictType
     * @return
     */
    @RequestMapping("/getDataDictByType")
    @ResponseBody
    public String getDataDictByType(String dictType) {
        return dataDictInit.getDataDictByType(dictType);
    }

    /**
     * 批量获取字典数据
     *
     * @param dictTypeList
     * @return
     */
    @RequestMapping("/getDataDictByTypeArr")
    @ResponseBody
    public AjaxResult getDataDictByTypeArr(@RequestBody List<String> dictTypeList) {
        Map<String, String> retMap = new HashMap();
        for (int i = 0; i < dictTypeList.size(); i++) {
            retMap.put(dictTypeList.get(i), dataDictInit.getDataDictByType(dictTypeList.get(i)));
        }
        return AjaxResult.success("success", retMap);
    }

}